# coding=utf-8
""" 
爬取百度图片的高清原图 
"""
import re
import urllib
import os
import requests


def getURL(keyword):
    url = r'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1530966732469_R&pv=&ic=0\
        &nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1530966732470%5E00_1349X662&word='
    # urllib.parse.quote 去除空格
    url_init = url + urllib.parse.quote(keyword, safe='/')
    return url_init

def get_response(url):
    if not url:
        print('执行结束')
        return [], ''
    try:
        response =requests.get(url).content
        # response.raise_for_status()
        # print(response)
        return response
    except Exception as e:
        print(e,"连接失败")

def getcontent(response):
    reg = re.compile(r'"objURL":"(.*?)",', re.S)
    return re.findall(reg,str(response))


def downloadPic(picUrl,fileaddr,count):
        try:
            pic = requests.get(picUrl, timeout=15)
            ss = str(count + 1) + '.jpg'
            with open(fileaddr + '%s.jpg' % ss, 'wb')as f:
                f.write(pic.content)
                # print(pic.content)
                print("写入第 %d 张图片成功" %count)
        except Exception as e:
            print(e,"下载 %s 张图片出错" % count,picUrl)
def getPicUrl(*args):

    content=set(getcontent(get_response(getURL(keyword))))
    count =1
    for i in content:
        if not os.path.exists(fileaddr):
            os.makedirs(fileaddr)
        try:
            downloadPic(i,fileaddr,count)
        except Exception as e:
            print(e)
            continue
        count +=1
def main():

    [getPicUrl(start_url) for start_url in start_urls]

if __name__ == '__main__':
    # keyword="苍井空"
    # fileaddr='F:/snake/pic/cangjinkong/'
    keyword = "鞠婧祎"
    fileaddr = 'F:/snake/pic/jujingwei/'
    start_urls = ["https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1531029741399_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E8%8B%8D%E8%80%81%E5%B8%88{}".format(i) for i in range(6, 20)]
    main()
    print("图片下载完成")